﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using eTerm.PlugIn.NCore;
using eTerm.MsSql.DbProvider;
using System.Configuration;

namespace eTerm.PlugIn.Launcher
{
    public partial class frmLauncher : Form
    {
        public frmLauncher()
        {
            InitializeComponent();
            BindEvent();
            this.Load += (o, e) => {
                this.Text = string.Format(@"{0} V{1}", AssemblyTitle, AssemblyVersion);
                new TaskBarUtil(this, notifyIcon1);
            };
        }

        #region 执行
        private delegate void InvokeDelegate();
        private void btnStart_Click(object sender, EventArgs e)
        {
            tbLog.Text = string.Empty;
            Context.Instance.DbProvider = new SqlDbProvider(ConfigurationManager.ConnectionStrings[@"DbProvider"].ConnectionString, ConfigurationManager.ConnectionStrings[@"DbProvider"].ProviderName);
            PlugInContext.Instance.Start(@"PlugIns");

            SetEnable();
        }

        private void SetEnable() {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new InvokeDelegate(SetEnable));
                return;
            }
            try
            {
                btnStart.Enabled = !btnStart.Enabled;
            }
            catch { }
        }

        private void BindEvent() {
            PlugInContext.Instance.OnCompleted += (o, e) =>
            {
            };

            PlugInContext.Instance.OnSearchedPlugIn += (o, e) =>
            {
                AppendString(string.Format(@"检测到插件:{0}[{1}]", e.PlugIn.PlugInName, e.PlugIn.GetType().FullName));
                //MsSqlDbLogProvider.Instance.INFO(e.PlugIn, string.Format(@"检测到插件:{0}[{1}]", e.PlugIn.PlugInName, e.PlugIn.GetType().FullName));
            };

            PlugInContext.Instance.OnExecuting += (o, e) =>
            {
                AppendString(string.Format(@"
开始执行插件:{1}[{2}]

**************************{0}*******************************", DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss"), e.PlugIn.PlugInName, e.PlugIn.GetType().FullName));
            };
            PlugInContext.Instance.OnException += (o, e) =>
            {
                AppendString(string.Format(@"   “{0}”[{1}] 分析失败。
        原因如下：
        {2}
", e.PlugIn.PlugInName, e.PlugIn.GetType().Name, e.Notify));
                MsSqlDbLogProvider.Instance.ERROR(e.PlugIn, string.Format(@"   “{0}”[{1}] 分析失败。
        原因如下：
        {2}
", e.PlugIn.PlugInName, e.PlugIn.GetType().Name, e.Notify));
            };
            PlugInContext.Instance.OnExecuted += (o, e) =>
            {
                AppendString(string.Format(@"   {0}[{1}] 分析完毕。
**************************{2}*******************************", e.PlugIn.PlugInName, e.PlugIn.GetType().Name, DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss")));
                MsSqlDbLogProvider.Instance.INFO(e.PlugIn, string.Format(@"   {0}[{1}] 分析完毕。", e.PlugIn.PlugInName, e.PlugIn.GetType().Name, DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss")));
            };
            PlugInContext.Instance.OnNotify += (o, e) =>
            {
                AppendString(string.Format(@"   {0} 插件输出： {1}{2}。
------------------------------------", e.PlugIn.PlugInName, e.Notify, e.NotifyType == EventType.NONE ? string.Empty : string.Format(@"[{0}]", string.IsNullOrEmpty(e.BusinessNo) ? e.PlugIn.GetType().FullName : e.BusinessNo)));
                MsSqlDbLogProvider.Instance.WRITELOG((int)e.NotifyType, e.PlugIn, string.Format(@"  {1}{2}。", e.PlugIn.PlugInName, e.Notify, e.NotifyType == EventType.NONE ? string.Empty : string.Format(@"[{0}]", string.IsNullOrEmpty(e.BusinessNo) ? e.PlugIn.GetType().FullName : e.BusinessNo)));
            };
            //SetToolButton(btnStart, false);
        }
        #endregion

        #region UI日志


        private delegate void AppendStringBack(string text);

        private void AppendString(string text)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new AppendStringBack(AppendString), text);
                return;
            }
            try
            {
                //if (notifyIcon1.Visible) return;
                if (tbLog.Lines.Length > 500)
                    tbLog.Text = string.Empty;

                tbLog.AppendText(text);
                tbLog.AppendText(@"
");
                tbLog.SelectionStart = tbLog.TextLength;
                tbLog.ScrollToCaret();
            }
            catch { }
        }

        #endregion

        #region 版本信息


        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }
        #endregion

        
    }
}
